package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"shao-mai/cache"
	"shao-mai/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carcode := c.PostForm("carcode")
	carname := c.PostForm("carname")
	carnum := c.PostForm("carnum")
	if carnum == "" || carname == "" || carcode == "" {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	car := models.Car{
		CarCode: carcode,
		CarName: carname,
		CarNum:  carnum,
	}
	models.DB.Create(&car)
	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	ordercode := c.PostForm("ordercode")
	fromaddress := c.PostForm("fromaddress")
	toaddress := c.PostForm("toaddress")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if ordercode == "" || fromaddress == "" || toaddress == "" || cid == 0 {
		c.JSONP(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	order := models.Order{
		OrderCode:   ordercode,
		FromAddress: fromaddress,
		ToAddress:   toaddress,
		Cid:         cid,
	}
	models.DB.Create(&order)
	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": order,
	})
	return
}
func OrderLst(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  cars.car_code as CarCode,
		  cars.car_name AS CarName,
		  cars.car_num as CarNum,
		  orders.order_code as OrderCode,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress

		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.id
	*/
	var order []models.OrderList
	redis_key := "order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Select(
				"cars.car_code as CarCode",
				"cars.car_name AS CarName",
				"cars.car_num as CarNum",
				"orders.order_code as OrderCode",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSONP(200, gin.H{
		"code":  200,
		"data":  order,
		"msg":   "查询成功",
		"total": total,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var order models.OrderList
	redis_key := "order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Select(
				"cars.car_code as CarCode",
				"cars.car_name AS CarName",
				"cars.car_num as CarNum",
				"orders.order_code as OrderCode",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Where("orders.id = ?", id).
			Debug().
			Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}

	//c.JSONP(200, gin.H{
	//	"code": 200,
	//	"data": order,
	//	"msg":  "查询成功",
	//})
	//return
	c.JSONP(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": order,
	})
	return
}
